package org.hhh.product.service.impl;

import org.hhh.basic.service.impl.BaseServiceImpl;
import org.hhh.product.domain.Product;
import org.hhh.product.domain.ProductDetail;
import org.hhh.product.mapper.ProductDetailMapper;
import org.hhh.product.mapper.ProductMapper;
import org.hhh.product.service.IProductService;
import org.hhh.user.domain.Logininfo;
import org.hhh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public void add(Product product) {
        //保存product信息
        productMapper.save(product);
        //保存product详细信息
        ProductDetail detail = product.getDetail();
        if (detail!=null){
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }
    }

    @Override
    public void onSale(List<Long> ids) {
        Map<String,Object> map=new HashMap<>();
        map.put("ids", ids);
        map.put("onSaleTime", new Date());
        productMapper.onSale(map);
    }

    @Override
    public void offSale(List<Long> ids) {Map<String,Object> map=new HashMap<>();
        map.put("ids", ids);
        map.put("offSaleTime", new Date());
        productMapper.offSale(map);
    }

    @Override
    public Product loadByIdDetail(Long id) {
        return productMapper.loadByIdDetail(id);
    }

    @Override
    public void adopt(Long id, Logininfo logininfo) {
        Product product = productMapper.loadById(id);
        //下架宠物
        product.setState(0);
        //更新下架时间
        product.setOffsaletime(new Date());
        productMapper.upadate(product);
        //@TODO 生成订单
        //@TODO 支付订单
    }
}
